<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css">
  <script src="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"zhangyh.fun","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="@[toc] mongoDB1. databases 基础命令1.1 数据库基本操作12345678# 查看当前数据库：db# 查看所有数据库：show dbs&#x2F;show databases# 切换数据库：use db_name# 删除当前数据库：db.dropDatabase()">
<meta property="og:type" content="article">
<meta property="og:title" content="mongoDB 使用">
<meta property="og:url" content="https://zhangyh.fun/2021/08/13/mongoDB%20%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="XiaoPang の Blog">
<meta property="og:description" content="@[toc] mongoDB1. databases 基础命令1.1 数据库基本操作12345678# 查看当前数据库：db# 查看所有数据库：show dbs&#x2F;show databases# 切换数据库：use db_name# 删除当前数据库：db.dropDatabase()">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-08-13T01:40:00.000Z">
<meta property="article:modified_time" content="2021-08-17T02:01:27.918Z">
<meta property="article:author" content="XiaoPang">
<meta property="article:tag" content="mongoDB">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://zhangyh.fun/2021/08/13/mongoDB%20%E4%BD%BF%E7%94%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>mongoDB 使用 | XiaoPang の Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?54b35e78bac318b678b24fbb33049203";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
  </script>


</head>

<body itemscope itemtype="http://schema.org/WebPage">

  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">XiaoPang の Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
    <script src="https://v1.hitokoto.cn/?c=f&encode=js&select=%23hitokoto" defer></script>

      <p class="site-subtitle" itemprop="description" id="hitokoto"><span id="hitokoto_text">记录生活中的点点滴滴</span></p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">19</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">11</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">34</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://zhangyh.fun/2021/08/13/mongoDB%20%E4%BD%BF%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="XiaoPang">
      <meta itemprop="description" content="少说，多做">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="XiaoPang の Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          mongoDB 使用
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于：</span>

              <time title="创建时间：2021-08-13 09:40:00" itemprop="dateCreated datePublished" datetime="2021-08-13T09:40:00+08:00">2021-08-13</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于：</span>
                <time title="修改时间：2021-08-17 10:01:27" itemprop="dateModified" datetime="2021-08-17T10:01:27+08:00">2021-08-17</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Nosql/" itemprop="url" rel="index"><span itemprop="name">Nosql</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
            <br>
            <span class="post-meta-item" title="本文字数">
            <span class="post-meta-item-icon">
            <i class="fa fa-file-word"></i>
            </span>
           <span class="post-meta-item-text">本文字数：</span>
           <span title="post.wordcount">
                2.7k 字
            </span>
            </span>
            <span class="post-meta-item" title="阅读时长">
            <span class="post-meta-item-icon">
            <i class="fa fa-clock"></i>
            </span>
          <span class="post-meta-item-text">阅读时长：</span>
           <span title="post.min2read">
                12 分钟
            </span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>@[toc]</p>
<h1 id="mongoDB"><a href="#mongoDB" class="headerlink" title="mongoDB"></a>mongoDB</h1><h2 id="1-databases-基础命令"><a href="#1-databases-基础命令" class="headerlink" title="1. databases 基础命令"></a>1. databases 基础命令</h2><h3 id="1-1-数据库基本操作"><a href="#1-1-数据库基本操作" class="headerlink" title="1.1 数据库基本操作"></a>1.1 数据库基本操作</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># 查看当前数据库：</span><br><span class="line">db</span><br><span class="line"># 查看所有数据库：</span><br><span class="line"><span class="keyword">show</span> dbs<span class="operator">/</span><span class="keyword">show</span> databases</span><br><span class="line"># 切换数据库：</span><br><span class="line">use db_name</span><br><span class="line"># 删除当前数据库：</span><br><span class="line">db.dropDatabase()</span><br></pre></td></tr></table></figure>
<span id="more"></span>

<h3 id="1-2-集合操作"><a href="#1-2-集合操作" class="headerlink" title="1.2 集合操作"></a>1.2 集合操作</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># 手动创建集合：</span><br><span class="line">db.createCollection(name,options)</span><br><span class="line">db.createCollection(&quot;stu&quot;)</span><br><span class="line">db.createCollection(&quot;sub&quot;, &#123;capped: <span class="literal">true</span>, size: <span class="number">10</span>&#125;)</span><br><span class="line"># 参数capped：默认值为<span class="literal">false</span>表示不设置上限，值为<span class="literal">true</span>表示设置上限</span><br><span class="line"># 参数size：当capped 值为<span class="literal">true</span>时，需要指定此参数，表示上限大小，当文件达到上限是，会将之前的数据覆盖</span><br><span class="line"># 查看集合：</span><br><span class="line"><span class="keyword">show</span> collections</span><br><span class="line"># 删除集合：</span><br><span class="line">db.collection_name.drop()</span><br></pre></td></tr></table></figure>
<ul>
<li>创建日期语句：<code>new Date(&quot;2021-07-07&quot;)</code></li>
<li>参数格式：<code>YYYY-MM-DD</code></li>
</ul>
<h3 id="1-3-添加数据"><a href="#1-3-添加数据" class="headerlink" title="1.3 添加数据"></a>1.3 添加数据</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"># 字段插入：</span><br><span class="line">db.collection_name.insert(&#123;&quot;name&quot;: &quot;zhangsan&quot;, &quot;age&quot;: <span class="number">18</span>&#125;)</span><br><span class="line">db.collection_name.save(&#123;&quot;name&quot;: &quot;zhangsan&quot;, &quot;age&quot;: <span class="number">18</span>&#125;)</span><br><span class="line"># 两个区别，<span class="keyword">insert</span>如果插入数据_id已存在，则报错。</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">WriteResult(&#123;</span></span><br><span class="line"><span class="comment">        &quot;nInserted&quot; : 0,</span></span><br><span class="line"><span class="comment">        &quot;writeError&quot; : &#123;</span></span><br><span class="line"><span class="comment">                &quot;code&quot; : 11000,</span></span><br><span class="line"><span class="comment">                &quot;errmsg&quot; : &quot;E11000 duplicate key error collection: test1.test1 index: _id_ dup key: &#123; : \&quot;101\&quot; &#125;&quot;</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">&#125;)</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># save如果插入数据_id已存在，则覆盖原来数据</span><br></pre></td></tr></table></figure>

<h3 id="1-4-数据查询"><a href="#1-4-数据查询" class="headerlink" title="1.4 数据查询"></a>1.4 数据查询</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">db.collection_name.find() 会查询集合下的所有元素</span><br><span class="line"></span><br><span class="line">db.collection_name.find(&#123;条件文档&#125;)</span><br><span class="line">db.collection_name.findOne(&#123;条件文档&#125;) 查询只返回一个</span><br><span class="line">db.collection_name.find(&#123;条件文档&#125;).pretty()</span><br><span class="line"></span><br><span class="line"><span class="operator">&gt;</span> db.test1.find(&#123;&quot;name&quot;: &quot;zhangsan1&quot;&#125;)</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;60e56aa72b2a0b777e5a1102&quot;), &quot;name&quot; : &quot;zhangsan1&quot;, &quot;age&quot; : <span class="number">20</span> &#125;</span><br><span class="line">&#123; &quot;_id&quot; : &quot;101&quot;, &quot;name&quot; : &quot;zhangsan1&quot;, &quot;age&quot; : <span class="number">30</span> &#125;</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;60e56b992b2a0b777e5a1103&quot;), &quot;name&quot; : &quot;zhangsan1&quot;, &quot;age&quot; : <span class="number">20</span> &#125;</span><br><span class="line"><span class="operator">&gt;</span> db.test1.findOne(&#123;&quot;name&quot;: &quot;zhangsan1&quot;&#125;)</span><br><span class="line">&#123;</span><br><span class="line">        &quot;_id&quot; : ObjectId(&quot;60e56aa72b2a0b777e5a1102&quot;),</span><br><span class="line">        &quot;name&quot; : &quot;zhangsan1&quot;,</span><br><span class="line">        &quot;age&quot; : <span class="number">20</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="operator">&gt;</span> db.test1.find(&#123;&quot;name&quot;: &quot;zhangsan1&quot;&#125;).pretty()</span><br><span class="line">&#123;</span><br><span class="line">        &quot;_id&quot; : ObjectId(&quot;60e56aa72b2a0b777e5a1102&quot;),</span><br><span class="line">        &quot;name&quot; : &quot;zhangsan1&quot;,</span><br><span class="line">        &quot;age&quot; : <span class="number">20</span></span><br><span class="line">&#125;</span><br><span class="line">&#123; &quot;_id&quot; : &quot;101&quot;, &quot;name&quot; : &quot;zhangsan1&quot;, &quot;age&quot; : <span class="number">30</span> &#125;</span><br><span class="line">&#123;</span><br><span class="line">        &quot;_id&quot; : ObjectId(&quot;60e56b992b2a0b777e5a1103&quot;),</span><br><span class="line">        &quot;name&quot; : &quot;zhangsan1&quot;,</span><br><span class="line">        &quot;age&quot; : <span class="number">20</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-5-数据更新"><a href="#1-5-数据更新" class="headerlink" title="1.5 数据更新"></a>1.5 数据更新</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">db.collection_name.update(<span class="operator">&lt;</span>query<span class="operator">&gt;</span>, <span class="operator">&lt;</span>update<span class="operator">&gt;</span>, &#123;multi: <span class="operator">&lt;</span><span class="type">boolean</span><span class="operator">&gt;</span>&#125;)</span><br><span class="line"># query：查询条件</span><br><span class="line"># update：更新操作符</span><br><span class="line"># multi：可选，默认<span class="literal">false</span>，表示只更新找到第一条记录，<span class="literal">true</span>表示把满足条件的文档全部更新。</span><br><span class="line"># 例：</span><br><span class="line">db.collect_name.update(&#123;&quot;name&quot;: &quot;hr&quot;&#125;, &#123;&quot;name&quot;: &quot;mnc&quot;&#125;) 更新一条</span><br><span class="line">会将满足条件内容替换成后面内容</span><br><span class="line"></span><br><span class="line"># 更新前：&#123; &quot;_id&quot; : ObjectId(&quot;60e56a9e2b2a0b777e5a1101&quot;), &quot;name&quot; : &quot;zhangsan&quot;, &quot;age&quot; : <span class="number">18</span> &#125;</span><br><span class="line"><span class="operator">&gt;</span> db.test1.update(&#123;&quot;name&quot;: &quot;zhangsan&quot;&#125;, &#123;&quot;name&quot;: &quot;lisi&quot;&#125;)</span><br><span class="line">WriteResult(&#123; &quot;nMatched&quot; : <span class="number">1</span>, &quot;nUpserted&quot; : <span class="number">0</span>, &quot;nModified&quot; : <span class="number">1</span> &#125;)</span><br><span class="line"><span class="operator">&gt;</span> db.test1.find()</span><br><span class="line"># 更新后：&#123; &quot;_id&quot; : ObjectId(&quot;60e56a9e2b2a0b777e5a1101&quot;), &quot;name&quot; : &quot;lisi&quot; &#125;</span><br><span class="line"></span><br><span class="line">db.collect_name.update(&#123;&quot;name&quot;: &quot;hr&quot;&#125;, &#123;$<span class="keyword">set</span>: &#123;name: &quot;hys&quot;&#125;&#125;) # 更新一条</span><br><span class="line"></span><br><span class="line"># $<span class="keyword">set</span>: 只替换对应键的值</span><br><span class="line"># 更新前：&#123; &quot;_id&quot; : ObjectId(&quot;60e572b22b2a0b777e5a1104&quot;), &quot;name&quot; : &quot;zhangsan2&quot;, &quot;age&quot; : <span class="number">20</span> &#125;</span><br><span class="line"><span class="operator">&gt;</span> db.test1.update(&#123;&quot;name&quot;: &quot;zhangsan2&quot;&#125;, &#123;$<span class="keyword">set</span>:&#123;&quot;name&quot;: &quot;lisi2&quot;&#125;&#125;)</span><br><span class="line">WriteResult(&#123; &quot;nMatched&quot; : <span class="number">1</span>, &quot;nUpserted&quot; : <span class="number">0</span>, &quot;nModified&quot; : <span class="number">1</span> &#125;)</span><br><span class="line"># 更新后：&#123; &quot;_id&quot; : ObjectId(&quot;60e572b22b2a0b777e5a1104&quot;), &quot;name&quot; : &quot;lisi2&quot;, &quot;age&quot; : <span class="number">20</span> &#125;</span><br><span class="line"></span><br><span class="line">db.collect_name.update(&#123;&#125;, &#123;$<span class="keyword">set</span>:&#123;gender: <span class="number">0</span>&#125;&#125;, &#123;multi: <span class="literal">true</span>&#125;) # 更新全部</span><br></pre></td></tr></table></figure>

<h3 id="1-6-数据删除"><a href="#1-6-数据删除" class="headerlink" title="1.6 数据删除"></a>1.6 数据删除</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">db.collection_name.remove(<span class="operator">&lt;</span>query<span class="operator">&gt;</span>, &#123;justOne: <span class="operator">&lt;</span><span class="type">boolean</span><span class="operator">&gt;</span>&#125;)</span><br><span class="line"># 参数query：可选，删除文档的条件</span><br><span class="line"># 参数justOne：可选，如果设为<span class="literal">true</span>或<span class="number">1</span>，则只删除一条，默认<span class="literal">false</span>，表示删除多条。</span><br></pre></td></tr></table></figure>

<h3 id="1-7-数据类型："><a href="#1-7-数据类型：" class="headerlink" title="1.7 数据类型："></a>1.7 数据类型：</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">object ID：文档ID</span><br><span class="line">相当于MySQL中的主键，保证文档的唯一性。可以自己设置，如果插入数据没有提供那么会自动生成一个独特的_id，</span><br><span class="line">objectID是一个12字节的十六进制数：</span><br><span class="line">前4个字节为当前时间戳</span><br><span class="line">之后3个字节是机器ID</span><br><span class="line">接下来两个字节是MongoDB的服务进程id</span><br><span class="line">最后3个字节是简单的增量值</span><br><span class="line"></span><br><span class="line">string：字符串，最常用，必须是有效的UTF-8</span><br><span class="line">boolean：存储一个布尔值，true/false</span><br><span class="line">integer：整数可以是32/64，这取决于服务器</span><br><span class="line">double：存储浮点型</span><br><span class="line">arrays：数据或列表，多个值存储到一个键</span><br><span class="line">object：用于嵌入式的文档，即一个值为一个文档</span><br><span class="line">null：存储null值</span><br><span class="line">timestamp：时间戳</span><br><span class="line">date：存储当前时间的UNIX时间格式。</span><br><span class="line">- 总结：与json数据类型相似。</span><br></pre></td></tr></table></figure>
<h3 id="1-8-查询指定字段数据，去重"><a href="#1-8-查询指定字段数据，去重" class="headerlink" title="1.8 查询指定字段数据，去重"></a>1.8 查询指定字段数据，去重</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.collection_name.distinct(&quot;key_name&quot;)</span><br></pre></td></tr></table></figure>
<h3 id="1-9-操作数据结果集"><a href="#1-9-操作数据结果集" class="headerlink" title="1.9 操作数据结果集"></a>1.9 操作数据结果集</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 统计查询数量</span><br><span class="line">db.collection_name.find().<span class="built_in">count</span>()</span><br><span class="line"># 结果集排序</span><br><span class="line"># 升序 key_name: <span class="number">1</span></span><br><span class="line">db.collection_name.find().sort(&#123;salary: <span class="number">1</span>&#125;)</span><br><span class="line"># 降序 key_name: <span class="number">-1</span></span><br><span class="line">db.collection_name.find().sort(&#123;salary: <span class="number">-1</span>&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="1-10-查询结果是否存在某个字段"><a href="#1-10-查询结果是否存在某个字段" class="headerlink" title="1.10 查询结果是否存在某个字段"></a>1.10 查询结果是否存在某个字段</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 查询不存在age字段，但是有gender字段</span><br><span class="line">db.collection_name.find(&#123;age: &#123;$<span class="keyword">exists</span>: <span class="literal">false</span>&#125;, gender: &#123;$<span class="keyword">exists</span>: <span class="literal">true</span>&#125;&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="1-11-分组查询-group"><a href="#1-11-分组查询-group" class="headerlink" title="1.11 分组查询 group"></a>1.11 分组查询 <code>group</code></h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"># aggregate</span><br><span class="line"># $<span class="keyword">group</span></span><br><span class="line"># 对应的聚合函数 $sum、$avg、$max、$min、$<span class="keyword">first</span>、$<span class="keyword">last</span></span><br><span class="line"># 与MySQL 分组对比</span><br><span class="line"># 例<span class="number">1</span>:</span><br><span class="line"><span class="keyword">select</span> id, <span class="built_in">avg</span>(salary) <span class="keyword">as</span> avg_salaey <span class="keyword">from</span> table_name <span class="keyword">where</span> id <span class="operator">&gt;</span> <span class="number">3</span> <span class="keyword">group</span> <span class="keyword">by</span> post;</span><br><span class="line">db.collection_name.aggregate(</span><br><span class="line">	&#123;&quot;$match&quot;: &#123;&quot;_id&quot;: &#123;&quot;$gt&quot;: <span class="number">3</span>&#125;&#125;&#125;,</span><br><span class="line">	&#123;&quot;$group&quot;: &#123;&quot;_id&quot;: &quot;$post&quot;, &quot;avg_salaey&quot;: &#123;&quot;$avg&quot;: &quot;$salary&quot;&#125;&#125;&#125;</span><br><span class="line">)</span><br><span class="line"># 例<span class="number">2</span>:</span><br><span class="line"># 多个字段分组</span><br><span class="line">db.collection_name.aggregate(</span><br><span class="line">	&#123;&quot;$match&quot;: &#123;&quot;TYPE_CODE&quot;: &#123;&quot;$regex&quot;: &quot;^1203&quot;&#125;, &quot;PRV&quot;: &#123;&quot;$in&quot;: [&quot;北京市&quot;, &quot;上海市&quot;]&#125;&#125;&#125;,</span><br><span class="line">	&#123;&quot;$group&quot;: &#123;<span class="string">&#x27;_id&#x27;</span>: &#123;<span class="string">&#x27;PRV&#x27;</span>: &quot;$PRV&quot;, &quot;TYPE_CODE&quot;: &quot;$TYPE_CODE&quot;&#125;, &quot;count&quot;: &#123;&quot;$sum&quot;: <span class="number">1</span>&#125;&#125;&#125;,</span><br><span class="line">	&#123;&quot;$match&quot;: &#123;&quot;count&quot;: &#123;&quot;$gt&quot;: <span class="number">1000</span>&#125;&#125;&#125;</span><br><span class="line">)</span><br><span class="line"># 注：<span class="keyword">match</span> 与 <span class="keyword">group</span> 没有特定顺序，可以进行多次条件查询。操作查询过程中，每一个操作像是一个管道接收上一个的数据进行处理在传给下一个。</span><br></pre></td></tr></table></figure>

<h2 id="2-运算符"><a href="#2-运算符" class="headerlink" title="2. 运算符"></a>2. 运算符</h2><h3 id="2-1-比较运算符"><a href="#2-1-比较运算符" class="headerlink" title="2.1 比较运算符"></a>2.1 比较运算符</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># 等于：默认是等于判断，没有运算符</span><br><span class="line"># 小于：$lt		(less than)</span><br><span class="line"># 小于等于：$lte	(less than equal)</span><br><span class="line"># 大于：$gt		(greater than)</span><br><span class="line"># 大于等于：$gte	(greater than equal)</span><br><span class="line"># 不等于：$ne</span><br><span class="line"></span><br><span class="line">db.stu.find(&#123;&quot;age&quot;: &#123;$gte: <span class="number">18</span>&#125;&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="2-2-范围运算符"><a href="#2-2-范围运算符" class="headerlink" title="2.2 范围运算符"></a>2.2 范围运算符</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># $<span class="keyword">in</span>，$nin 判断是否在某个范围内</span><br><span class="line"># 查询年龄<span class="number">18</span>, <span class="number">28</span>的学生：</span><br><span class="line">db.stu.find(&#123;&quot;age&quot;: &#123;$<span class="keyword">in</span>: [<span class="number">18</span>, <span class="number">28</span>]&#125;&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="2-3-逻辑运算符"><a href="#2-3-逻辑运算符" class="headerlink" title="2.3 逻辑运算符"></a>2.3 逻辑运算符</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># <span class="keyword">and</span>：直接在json文档中写多个条件</span><br><span class="line"># 查询年龄大于等于<span class="number">18</span>，并且性别为<span class="literal">true</span>的学生</span><br><span class="line">db.stu.find(&#123;age: &#123;$gte: <span class="number">18</span>&#125;, gender: <span class="literal">true</span>&#125;)</span><br><span class="line"># <span class="keyword">or</span>：使用$<span class="keyword">or</span>，值为数组，数组中元素为json</span><br><span class="line"># 查询年龄大于<span class="number">18</span>，或者性别为<span class="literal">false</span>的学生</span><br><span class="line">db.stu.find(&#123;$<span class="keyword">or</span>: [&#123;age: &#123;$gt: <span class="number">18</span>&#125;&#125;, &#123;gender: <span class="literal">false</span>&#125;]&#125;)</span><br><span class="line"># 查询年龄大于<span class="number">18</span>或性别为<span class="literal">true</span>，并且姓名是gj</span><br><span class="line">db.stu.find(&#123;$<span class="keyword">or</span>: [&#123;age: &#123;$gt: <span class="number">18</span>&#125;&#125;, &#123;gender: <span class="literal">true</span>&#125;], name: &quot;gj&quot;&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="2-4-正则表达式"><a href="#2-4-正则表达式" class="headerlink" title="2.4 正则表达式"></a>2.4 正则表达式</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 使用<span class="operator">/</span><span class="operator">/</span> 或 $regex 编写正则表达式</span><br><span class="line"># 查询sku键中包含abc 的数据</span><br><span class="line">db.collect_name.find(&#123;sku: <span class="operator">/</span>abc<span class="operator">/</span>&#125;)</span><br><span class="line"># 查询sku键以abc开头的值对应的数据：</span><br><span class="line">db.collect_name.find(&#123;sku: <span class="operator">/</span><span class="operator">^</span>abc<span class="operator">/</span>&#125;)</span><br><span class="line"># 查询sku键以<span class="number">789</span>结尾的值对应的数据：</span><br><span class="line">db.collect_name.find(&#123;sku: &#123;$regex: <span class="string">&#x27;789$&#x27;</span>&#125;&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="2-5-limit、skip"><a href="#2-5-limit、skip" class="headerlink" title="2.5 limit、skip"></a>2.5 limit、skip</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"># limit()：用于读取指定数量的文档</span><br><span class="line">db.collect_name.find().limit(number)</span><br><span class="line"># 查询前<span class="number">2</span>条学生记录</span><br><span class="line">db.stu.find().limit(<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line"># <span class="keyword">skip</span>()：用于跳过指定数量的文档，取之后的数据</span><br><span class="line">db.collect_time.find().<span class="keyword">skip</span>(number)</span><br><span class="line"># 查询前两条之后的数据</span><br><span class="line">db.collect_time.find().<span class="keyword">skip</span>(<span class="number">2</span>)</span><br><span class="line"># 同时使用：</span><br><span class="line"></span><br><span class="line">db.collect_time.find().limit(<span class="number">2</span>).<span class="keyword">skip</span>(<span class="number">3</span>)</span><br><span class="line"># 等价</span><br><span class="line">db.collect_time.find().<span class="keyword">skip</span>(<span class="number">3</span>).limit(<span class="number">2</span>)</span><br><span class="line"># 注意：当需要limit，<span class="keyword">skip</span>同时使用时，如果查询的集合元素数据量很大的情况下，应该先<span class="keyword">skip</span>，之后在limit</span><br></pre></td></tr></table></figure>

<h3 id="2-6-自定义查询"><a href="#2-6-自定义查询" class="headerlink" title="2.6 自定义查询"></a>2.6 自定义查询</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># 使用$<span class="keyword">where</span> 后面写一个函数，返回满足条件的数据</span><br><span class="line"># 查询年龄大于<span class="number">30</span>的学生</span><br><span class="line">db.stu.find(&#123;$<span class="keyword">where</span>: <span class="keyword">function</span>()&#123;<span class="keyword">return</span> this.age <span class="operator">&gt;</span> <span class="number">30</span>;&#125;&#125;) </span><br></pre></td></tr></table></figure>

<h3 id="2-7-查询投影"><a href="#2-7-查询投影" class="headerlink" title="2.7 查询投影"></a>2.7 查询投影</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"># 只需要返回指定字段，默认不写，返回全部字段</span><br><span class="line">db.collect_name.find(&#123;条件&#125;, &#123;字段名称<span class="number">1</span>: <span class="number">1</span>,....&#125;)</span><br><span class="line"># 返回符合条件的数据，其中返回字段有_id，字段名称<span class="number">1</span></span><br><span class="line"># 特殊：如果返回不包含_id，则需要给_id<span class="operator">=</span><span class="number">0</span>。</span><br><span class="line"># 注意：字段名称为<span class="number">0</span>，只对_id有效，其他字段置<span class="number">0</span>会报错。</span><br><span class="line"># 如果没有条件，需要查询全部，则条件为空&#123;&#125;，如果不写则默认第一个为条件，不会产生查询投影，只返回指定字段。</span><br><span class="line"><span class="operator">&gt;</span> db.test1.find(&#123;name: &quot;zhangsan1&quot;&#125;)</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;60e56aa72b2a0b777e5a1102&quot;), &quot;name&quot; : &quot;zhangsan1&quot;, &quot;age&quot; : <span class="number">20</span> &#125;</span><br><span class="line">&#123; &quot;_id&quot; : &quot;101&quot;, &quot;name&quot; : &quot;zhangsan1&quot;, &quot;age&quot; : <span class="number">30</span> &#125;</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;60e56b992b2a0b777e5a1103&quot;), &quot;name&quot; : &quot;zhangsan1&quot;, &quot;age&quot; : <span class="number">20</span> &#125;</span><br><span class="line"><span class="operator">&gt;</span> db.test1.find(&#123;name: &quot;zhangsan1&quot;&#125;, &#123;name:<span class="number">1</span>&#125;)</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;60e56aa72b2a0b777e5a1102&quot;), &quot;name&quot; : &quot;zhangsan1&quot; &#125;</span><br><span class="line">&#123; &quot;_id&quot; : &quot;101&quot;, &quot;name&quot; : &quot;zhangsan1&quot; &#125;</span><br><span class="line">&#123; &quot;_id&quot; : ObjectId(&quot;60e56b992b2a0b777e5a1103&quot;), &quot;name&quot; : &quot;zhangsan1&quot; &#125;</span><br><span class="line"><span class="operator">&gt;</span> db.test1.find(&#123;name: &quot;zhangsan1&quot;&#125;, &#123;name:<span class="number">1</span>, _id: <span class="number">0</span>&#125;)</span><br><span class="line">&#123; &quot;name&quot; : &quot;zhangsan1&quot; &#125;</span><br><span class="line">&#123; &quot;name&quot; : &quot;zhangsan1&quot; &#125;</span><br><span class="line">&#123; &quot;name&quot; : &quot;zhangsan1&quot; &#125;</span><br><span class="line"><span class="operator">&gt;</span> db.test1.find(&#123;name: &quot;zhangsan1&quot;&#125;, &#123;name:<span class="number">1</span>, age: <span class="number">0</span>&#125;)</span><br><span class="line">Error: error: &#123;</span><br><span class="line">        &quot;ok&quot; : <span class="number">0</span>,</span><br><span class="line">        &quot;errmsg&quot; : &quot;Projection cannot have a mix of inclusion and exclusion.&quot;,</span><br><span class="line">        &quot;code&quot; : <span class="number">2</span>,</span><br><span class="line">        &quot;codeName&quot; : &quot;BadValue&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-创建索引"><a href="#3-创建索引" class="headerlink" title="3. 创建索引"></a>3. 创建索引</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"># 默认情况下索引字段的值可以相同</span><br><span class="line">db.collect_name.ensureIndex(&#123;属性: <span class="number">1</span>&#125;)，<span class="number">1</span>表示升序；<span class="number">-1</span>表示降序</span><br><span class="line"></span><br><span class="line"># 测试：插入<span class="number">10</span>w条数据到数据库中</span><br><span class="line"><span class="keyword">for</span>(i<span class="operator">=</span><span class="number">0</span>;i<span class="operator">&lt;</span><span class="number">100000</span>;i<span class="operator">+</span><span class="operator">+</span>)&#123;db.test2.insert(&#123;name: <span class="string">&#x27;test&#x27;</span> <span class="operator">+</span> i, age: i&#125;)&#125;</span><br><span class="line"></span><br><span class="line"># 创建索引之前：</span><br><span class="line">db.test2.find(&#123;name: <span class="string">&#x27;test90000&#x27;</span>&#125;)</span><br><span class="line">db.test2.find(&#123;name: <span class="string">&#x27;test90000&#x27;</span>&#125;).explain(<span class="string">&#x27;executionStats&#x27;</span>)</span><br><span class="line"></span><br><span class="line"># 创建索引后：</span><br><span class="line">db.test2.find(&#123;name: <span class="string">&#x27;test90000&#x27;</span>&#125;)</span><br><span class="line">db.test2.find(&#123;name: <span class="string">&#x27;test90000&#x27;</span>&#125;).explain(<span class="string">&#x27;executionStats&#x27;</span>)</span><br><span class="line"></span><br><span class="line"># 创建唯一索引</span><br><span class="line"><span class="keyword">unique</span><span class="operator">=</span><span class="literal">true</span></span><br><span class="line">创建唯一索引之前，应该保证对应字段不重复。否则创建索引失败。</span><br><span class="line">db.collection_name.ensureIndex(&#123;name: <span class="number">1</span>&#125;, &#123;<span class="keyword">unique</span>: <span class="literal">true</span>&#125;)</span><br><span class="line"></span><br><span class="line"># 创建联合索引</span><br><span class="line">db.collection_name.ensureIndex(&#123;name: <span class="number">1</span>, age: <span class="number">1</span>&#125;)</span><br><span class="line"></span><br><span class="line"># 查看当前集合的所有索引</span><br><span class="line">db.collection_name.getIndexes()</span><br><span class="line"></span><br><span class="line"># 删除索引</span><br><span class="line">db.collection_name.dropIndex(<span class="string">&#x27;索引名称&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="4-数据备份和恢复"><a href="#4-数据备份和恢复" class="headerlink" title="4. 数据备份和恢复"></a>4. 数据备份和恢复</h2><h3 id="4-1-数据备份"><a href="#4-1-数据备份" class="headerlink" title="4.1 数据备份"></a>4.1 数据备份</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">mongodump -h dbhost -d dbname -o dbdirectory</span><br><span class="line"><span class="meta">#</span><span class="bash"> -h：服务器地址，可以指定端口号</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> -d：需要备份的数据库名称</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> -o：备份的数据存放位置，此目录存放着备份出来的数据</span></span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 例：</span></span><br><span class="line">mongodump -h 192.168.0.1:27017 -d test1 -o ~/Desktop/test1bak</span><br></pre></td></tr></table></figure>

<h3 id="4-2-数据恢复"><a href="#4-2-数据恢复" class="headerlink" title="4.2 数据恢复"></a>4.2 数据恢复</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">mongorestore -h dbhost -d dbname --dir dbdirectory</span><br><span class="line"><span class="meta">#</span><span class="bash"> -h：服务器地址，可以指定端口号</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> -d：需要恢复的数据库名称</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> --dir：备份的数据存放位置</span></span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 例：</span></span><br><span class="line">mongorestore -h 192.168.0.1:27017 -d test1 --dir ~/Desktop/test1bak/test</span><br></pre></td></tr></table></figure>


    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>XiaoPang
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://zhangyh.fun/2021/08/13/mongoDB%20%E4%BD%BF%E7%94%A8/" title="mongoDB 使用">https://zhangyh.fun/2021/08/13/mongoDB 使用/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://zhangyh.fun/2021/08/13/mongoDB%20%E4%BD%BF%E7%94%A8/"  aria-label="复制成功！"></i></span>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>
<script>
    var clipboard = new Clipboard('.fa-clipboard');
      $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({
          title: "",
          text: '复制成功',
          icon: "success",
          showConfirmButton: true
          });
        });
    });
</script>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/mongoDB/" rel="tag"><i class="fa fa-tag"></i> mongoDB</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/13/%E4%B8%BB%E6%9C%BA%E8%AE%BF%E9%97%AE%E8%99%9A%E6%8B%9F%E6%9C%BA%E7%AB%AF%E5%8F%A3/" rel="prev" title="主机访问虚拟机端口">
      <i class="fa fa-chevron-left"></i> 主机访问虚拟机端口
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/13/python%20%E6%93%8D%E4%BD%9Credis,%20%E9%9B%86%E5%90%88%EF%BC%88set%EF%BC%89%E7%B1%BB%E5%9E%8B/" rel="next" title="python 操作redis, 集合（set）类型">
      python 操作redis, 集合（set）类型 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    

  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#mongoDB"><span class="nav-number">1.</span> <span class="nav-text">mongoDB</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-databases-%E5%9F%BA%E7%A1%80%E5%91%BD%E4%BB%A4"><span class="nav-number">1.1.</span> <span class="nav-text">1. databases 基础命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E6%95%B0%E6%8D%AE%E5%BA%93%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="nav-number">1.1.1.</span> <span class="nav-text">1.1 数据库基本操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E9%9B%86%E5%90%88%E6%93%8D%E4%BD%9C"><span class="nav-number">1.1.2.</span> <span class="nav-text">1.2 集合操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E6%B7%BB%E5%8A%A0%E6%95%B0%E6%8D%AE"><span class="nav-number">1.1.3.</span> <span class="nav-text">1.3 添加数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-%E6%95%B0%E6%8D%AE%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.1.4.</span> <span class="nav-text">1.4 数据查询</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-5-%E6%95%B0%E6%8D%AE%E6%9B%B4%E6%96%B0"><span class="nav-number">1.1.5.</span> <span class="nav-text">1.5 数据更新</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-6-%E6%95%B0%E6%8D%AE%E5%88%A0%E9%99%A4"><span class="nav-number">1.1.6.</span> <span class="nav-text">1.6 数据删除</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-7-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%EF%BC%9A"><span class="nav-number">1.1.7.</span> <span class="nav-text">1.7 数据类型：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-8-%E6%9F%A5%E8%AF%A2%E6%8C%87%E5%AE%9A%E5%AD%97%E6%AE%B5%E6%95%B0%E6%8D%AE%EF%BC%8C%E5%8E%BB%E9%87%8D"><span class="nav-number">1.1.8.</span> <span class="nav-text">1.8 查询指定字段数据，去重</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-9-%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%9C%E9%9B%86"><span class="nav-number">1.1.9.</span> <span class="nav-text">1.9 操作数据结果集</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-10-%E6%9F%A5%E8%AF%A2%E7%BB%93%E6%9E%9C%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%E6%9F%90%E4%B8%AA%E5%AD%97%E6%AE%B5"><span class="nav-number">1.1.10.</span> <span class="nav-text">1.10 查询结果是否存在某个字段</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-11-%E5%88%86%E7%BB%84%E6%9F%A5%E8%AF%A2-group"><span class="nav-number">1.1.11.</span> <span class="nav-text">1.11 分组查询 group</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">1.2.</span> <span class="nav-text">2. 运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E6%AF%94%E8%BE%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">1.2.1.</span> <span class="nav-text">2.1 比较运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E8%8C%83%E5%9B%B4%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">1.2.2.</span> <span class="nav-text">2.2 范围运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">1.2.3.</span> <span class="nav-text">2.3 逻辑运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-number">1.2.4.</span> <span class="nav-text">2.4 正则表达式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-limit%E3%80%81skip"><span class="nav-number">1.2.5.</span> <span class="nav-text">2.5 limit、skip</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.2.6.</span> <span class="nav-text">2.6 自定义查询</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-7-%E6%9F%A5%E8%AF%A2%E6%8A%95%E5%BD%B1"><span class="nav-number">1.2.7.</span> <span class="nav-text">2.7 查询投影</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%88%9B%E5%BB%BA%E7%B4%A2%E5%BC%95"><span class="nav-number">1.3.</span> <span class="nav-text">3. 创建索引</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E6%95%B0%E6%8D%AE%E5%A4%87%E4%BB%BD%E5%92%8C%E6%81%A2%E5%A4%8D"><span class="nav-number">1.4.</span> <span class="nav-text">4. 数据备份和恢复</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E6%95%B0%E6%8D%AE%E5%A4%87%E4%BB%BD"><span class="nav-number">1.4.1.</span> <span class="nav-text">4.1 数据备份</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E6%95%B0%E6%8D%AE%E6%81%A2%E5%A4%8D"><span class="nav-number">1.4.2.</span> <span class="nav-text">4.2 数据恢复</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="XiaoPang"
      src="/images/avatar.gif">
  <p class="site-author-name" itemprop="name">XiaoPang</p>
  <div class="site-description" itemprop="description">少说，多做</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">34</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/zhang123-123" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhang123-123" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/1741290057@qq.com" title="E-Mail → 1741290057@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


<!-- CloudCalendar -->
<div class="widget-wrap" style="width: 90%;margin-left: auto;margin-right: auto; opacity: 0.97;">
	<div class="widget" id="CloudCalendar"></div>
</div>
      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">XiaoPang</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
    <script type="text/javascript" color="0,0,0" opacity='0.5' zIndex="-2" count="200" src="/js/my_js/canvas-nest.min.js"></script>
    

</body>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/my_js/clicklove.js"></script>

<!-- calendar widget -->

    <script src="/lib/calendar/calendar.min.js"></script>
    <script src="/lib/calendar/languages.min.js"></script>
    <script type="text/javascript">
    $(function() {
        $('#CloudCalendar').aCalendar('zh-CN',
            $.extend(
                '', {
                    single:true,
                    root:'/calendar/'
                }
            )
        );
    });
    </script>


</html>
